package com.nsl.my_youlai.bbu_return.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.nsl.my_youlai.bbu_return.model.bo.TransformSheetRecordBo;
import com.nsl.my_youlai.bbu_return.model.vo.TransformSheetRecordString;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class LoadTransferInfoByStringImpl {
    public void readTransferInfoSheet(String excelPath, List<TransformSheetRecordString> transformSheetRecordStringList){
        EasyExcel.read(excelPath, TransformSheetRecordString.class, new ReadListener<TransformSheetRecordString>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TransformSheetRecordString> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

            @Override
            public void invoke(TransformSheetRecordString data, AnalysisContext analysisContext) {
                cachedDataList.add(data);
                transformSheetRecordStringList.add(data);
                if (cachedDataList.size() >= BATCH_COUNT) {
                    saveData();
                    // 存储完成清理 list
                    cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {

            }
        }).sheet(1).headRowNumber(2).doRead();
    }

    public Set<Integer> getTransferNodeIdSet(List<TransformSheetRecordString> transformSheetRecordStringList){
        Set<Integer> transferNodeIdSet=new HashSet<>();
        for (TransformSheetRecordString transformSheetRecordString : transformSheetRecordStringList) {
            Integer nodeId=null;
            try{
                nodeId=Integer.parseInt(transformSheetRecordString.getNodeId());
            }catch (Exception e){
                continue;
            }
            transferNodeIdSet.add(nodeId);
        }
        return transferNodeIdSet;
    }



    public List<TransformSheetRecordBo> toBoList(List<TransformSheetRecordString> stringList){
        List<TransformSheetRecordBo> boList=new ArrayList<>();
        return null;
    }
}
